from django.http import JsonResponse, HttpResponse
from django.shortcuts import render

# Create your views here.
from jie.models import JieInfo
from user.models import UserInfo


def index(request):
    jieinfo = JieInfo.objects.all()
    list1 = []
    for i in jieinfo:
        content = {
            'user_name': i.jie_user.user_name,
            'user_photo': i.jie_user.user_photo.url,
            'jie_section': i.jie_section,
            'jie_title': i.jie_titile,
            'jie_date': str(i.jie_date),
            'jie_kiss': i.jie_kiss,
            'jie_comment': i.jie_comment,
        }
        list1.append(content)
        list1.reverse()
    request.session['jieinfo'] = list1
    return render(request, 'index.html', content)


def login(request):
    request.session.clear()
    return render(request, 'user/login.html')


def reg(request):
    return render(request, 'user/reg.html')


def doreg(request):
    # if request.session.get('userinfo'):
    #     # 登录状态不允许注册。你可以修改这条原则！
    #     return redirect("/index/")
    if request.method == "POST":
        message = "请检查填写的内容！"
        email = request.POST.get('email')
        username = request.POST.get('username')
        password1 = request.POST.get('pass')
        password2 = request.POST.get('repass')
        if password1 != password2:  # 判断两次密码是否相同
            message = "两次输入的密码不同！"
            return render(request, 'user/reg.html')
        else:
            same_name_user = UserInfo.objects.filter(user_name=username)
            if same_name_user:  # 用户名唯一
                message = '用户已经存在，请重新选择用户名！'
                return render(request, 'user/reg.html')
            same_email_user = UserInfo.objects.filter(user_email=email)
            if same_email_user:  # 邮箱地址唯一
                message = '该邮箱地址已被注册，请使用别的邮箱！'
                return render(request, 'user/reg.html')

            # 当一切都OK的情况下，创建新用户

            new_user = UserInfo()
            new_user.user_email = email
            new_user.user_name = username
            new_user.user_password = password1
            new_user.user_repassword = password2
            new_user.save()
            return render(request, 'user/login.html')  # 自动跳转到登录页面
    return render(request, 'user/reg.html')


def dologin(request):
    if request.method == "POST":
        username = request.POST.get('email')
        password = request.POST.get('pass')
        message = "所有字段都必须填写！"
        if username and password:  # 确保用户名和密码都不为空
            username = username.strip()
            try:
                ret = UserInfo.objects.get(user_email=username)
                if ret.user_password == int(password):
                    content = {
                        'user_id': ret.user_id,
                        'user_email': ret.user_email,
                        'user_name': ret.user_name,
                        'user_city': ret.user_city,
                        'user_gender': ret.user_gender,
                        'user_lable': ret.user_lable,
                        'user_photo': ret.user_photo.url,
                    }
                    # 将信息存入session
                    request.session['userinfo'] = content
                    return render(request, 'index.html', content)
                else:
                    message = "密码不正确！"
            except:
                message = "用户名不存在！"
        return render(request, 'user/login.html', {"message": message})
    return render(request, 'user/login.html')


def user_index(request):
    # 从session中取得数据
    userinfo = request.session.get('userinfo')
    if not (userinfo):
        return render(request, 'user/login.html')
    return render(request, 'user/index.html', userinfo)


def activate(request):
    # 从session中取得数据
    userinfo = request.session.get('userinfo')
    if not (userinfo):
        return render(request, 'user/login.html')
    return render(request, 'user/activate.html', userinfo)


def forget(request):
    # 从session中取得数据
    userinfo = request.session.get('userinfo')
    if not (userinfo):
        return render(request, 'user/login.html')

    return render(request, 'user/forget.html', userinfo)


def home(request):
    # 从session中取得数据
    userinfo = request.session.get('userinfo')
    if not (userinfo):
        return render(request, 'user/login.html')

    return render(request, 'user/home.html', userinfo)


def message(request):
    # 从session中取得数据
    userinfo = request.session.get('userinfo')
    if not (userinfo):
        return render(request, 'user/login.html')

    return render(request, 'user/message.html', userinfo)


def set(request):
    # 从session中取得数据
    userinfo = request.session.get('userinfo')
    if not (userinfo):
        return render(request, 'user/login.html')

    return render(request, 'user/set.html', userinfo)


def logout(request):
    if not request.session.get('userinfo'):  # 如果本来就未登录，也就没有登出一说
        return render(request, 'index.html')
    request.session.flush()
    return render(request, 'index.html')


def doset(request):
    s = request.session.get('userinfo')
    uid = s['user_id']

    # 根据id获得对应的用户对象
    u = UserInfo.objects.get(user_id=uid)
    u.user_email = request.POST.get('email')
    u.user_name = request.POST.get('username')
    gender = request.POST.get('sex')
    if gender == 'male':
        u.user_gender = True
    else:
        u.user_gender = False

    u.user_city = request.POST.get('city')
    u.user_lable = request.POST.get('sign')
    u.save()
    # 将改变的头像路径写入session
    s['user_email'] = u.user_email
    s['user_name'] = u.user_name
    s['user_city'] = u.user_city
    s['user_gender'] = u.user_gender
    s['user_photo'] = u.user_photo.url
    s['user_lable'] = u.user_lable

    request.session['userinfo'] = s
    return render(request, 'user/set.html')


def uploadphoto(request):
    # 从session中获取当前用户的id
    s = request.session.get('userinfo')
    uid = s['user_id']
    # 根据id获得对应的用户对象
    u = UserInfo.objects.get(user_id=uid)

    # 保存上传的文件
    u.user_photo = request.FILES.get('photo')

    # 将上传文件保存到DB
    u.save()

    # 将改变的头像路径写入session
    s['user_photo'] = u.user_photo.url
    request.session['userinfo'] = s

    content = {
        'user_photo': u.user_photo.url,
    }

    ret = JsonResponse(content)

    return HttpResponse(ret)
